﻿<script language="JavaScript" type="text/javascript" >

// It contains the function which to initiate data using in mark table.
// It also has the functions to set sent scores, set uneditable scores 
// and contains some utility functions to convert data from digital score to comment score 
// and reverse.
  
var timetime1;
var timetime2;
function catchEnter(e)
{
	  	
    if (!e) e = window.event; 
    var code = (e.keyCode) ? e.keyCode : e.which;
	if ((code == 13) || (code == 3) || (code==9)) 
	{
		return false;
	}	
	else return true;
}

window.onload = function() { 
	document.onkeydown = catchEnter;		
};
window.onerror= function(){
	haveError=true;	
	//window.location.reload();
};	
var MAX_COL ={{MAX_COL}};
var MAX_VIEW={{MAX_VIEW}};
var SIZE  = MAX_COL*3+5;
var temp = "{{enableChangeMark}}";
if (temp == "True") enableChangeMark = true;
else enableChangeMark = false;

temp = "{{enableSendSMS}}";
if (temp == "True") enableSendSMS = 1;
else enableSendSMS = 0;
	
	
if ("{{selectedTerm.number}}" == 2)
	isSecondTerm = 1;
else	
	isSecondTerm = 0; 
var isComment="{{selectedSubject.nx}}"=="True";
var idTeacher = "{{selectedSubject.teacher_id.id}}";
var mainInput=document.getElementById("mainInput");
var maxColMieng    ={{maxColMieng}};
var maxCol15Phut   ={{maxCol15Phut}};
var maxColMotTiet  ={{maxColMotTiet}};
var firstClick=true;

	//window.addeventListener('keydown', function(e) {
	//if ((e.keyCode==38) || (e.keyCode==38))
	//	e.preventDefault()
	//}, false);

var length={{lengthList}} + 1;
var oldAr     = new Array(length);
var newAr     = new Array(length); 
var enableEdit= new Array(length);
var tempNewAr = new Array(length); 

var sentMark  = new Array(length);
var tempSent  = new Array(length);

var idAr  = new Array(length);
var hk1Ar = new Array(length);
var messageNote	 = new Array(length);
var mgHK1 = new Array(length);
var mgHK2 = new Array(length);

var indexId = 0;
var indexHk1= 0;
var latestTime =0;
//var position  = "{{user.userprofile.position}}";
//che do lam viec
var activeMode = 0;
var currentId =0;
var numberId  =0;	
var haveError = false;
var timeToEdit = {{timeToEdit}};
var timeNow    = {{timeNow}}/60.0;

for(var i=1;i<length;i++)
{	
	oldAr[i]      = new Array(SIZE);
	newAr[i] 	  = new Array(SIZE);
	tempNewAr[i]  = new Array(SIZE);
	sentMark[i]   = new Array(SIZE);
	tempSent[i]   = new Array(SIZE);
	enableEdit[i] = new Array(SIZE);
	mgHK1[i]  = false;
	mgHK2[i]  = false;
}	

for(var i=1;i<length;i++)
	for(var j=1;j<SIZE;j++)
		{
			newAr[i][j]=-1;
			oldAr[i][j]=-1;
			enableEdit[i][j]=0;
			sentMark[i][j]=0;	
		}
		
	intiateEditable();

function intiateEditable()
{
	for (var i=1;i<length;i++)
		for (var j=1;j<SIZE;j++)
		{
			/*
			var name= i*100+j;
			var temp = document.getElementById(name);
			if (temp.value ==undefined)
				{
					table.rows[i+2].cells[j+4].className="noedit";
					enableEdit[i][j]=0;	
				}
			else
				enableEdit[i][j]=1;
			*/
			if ((!isComment) && (j<=3*MAX_COL+1))
				enableEdit[i][j]=1;
			else	
			if ((isComment) && (j<=3*MAX_COL+2) && (!isSecondTerm))
				enableEdit[i][j]=1;
			else	
			if ((isComment) && 
				((j==3*MAX_COL+3) || (j==3*MAX_COL+4)|| (j<=3*MAX_COL+1)) && 
				 (isSecondTerm))
				enableEdit[i][j]=1;							
		}
}	
function setArrayId(id)
{
	indexId++;
	idAr[indexId] = id;
}
function checkSent(x)
{
	if (x=='1')
		return 1;
	else 
		return 0;
}
function setSentMark(str)
{
	//alert(str);
	var sents=str.split('|')
	for (var i in sents){
		var ss=sents[i].split('*')
		for (var j in ss){		
			if (i<3)
				sentMark[indexId][parseInt(i)*MAX_COL+parseInt(j)+1]=checkSent(ss[j]);
			else
			if (isSecondTerm && (3*MAX_COL+parseInt(i)-2==3*MAX_COL+2))
				sentMark[indexId][3*MAX_COL+3]=checkSent(ss[j]);
			else	
				sentMark[indexId][3*MAX_COL+parseInt(i)-2]=checkSent(ss[j]);
		}		
	}
}

function setSentHk1(str)
{
	//alert(str);
	var sents=str.split('|')
	if (sents[4])
		sentMark[indexId][3*MAX_COL+2]=1;
}

function convertToDigit(value)
{
	var value1=value.toLowerCase();
	
	if (value1=="đ")
		return 7
	else	
	if (value1=='cđ')	
		return 1
	else
		return -1;
}
function convertToChar(value)
{
	value=value.trim();
	
	if (value=="")
		value=-1;
		
	if (value>=5)
		return "Đ";
	else
	if (value>=0)			
		return "CĐ";
	else
		return "";			
}

function convertToComment()
{
	console.log("chao");
	for (var i=1;i<length;i++)
		for(var j=1;j<SIZE;j++)
			if (table.rows[i+2].cells.length>j+5)			
				table.rows[i+2].cells[j+4].innerHTML = convertToChar(table.rows[i+2].cells[j+4].innerHTML)
}	
function setEditable(data)
{
	
	if (!enableChangeMark)
		return;
	var datas=data.split('|');			
	for (var i in datas){
		var ds=datas[i].split('*')
		for (var j in ds){				
			if (ds[j]=="")
				time=timeNow;					
			else
				time=parseInt(ds[j]);
			//alert(timeNow-time);	
			if ((timeNow-time) >timeToEdit+0.00001)
			{
				if (i<3)
					enableEdit[indexId][parseInt(i)*MAX_COL+parseInt(j)+1]=0;
				else
				if (isSecondTerm && (3*MAX_COL+parseInt(i)-2==3*MAX_COL+2))
					enableEdit[indexId][3*MAX_COL+3]=0;
				else
					enableEdit[indexId][3*MAX_COL+parseInt(i)-2]=0;
			}
			else{
				if (i<3)
					enableEdit[indexId][parseInt(i)*MAX_COL+parseInt(j)+1]=1;
				else	
				if (isSecondTerm && (3*MAX_COL+parseInt(i)-2==3*MAX_COL+2))
					enableEdit[indexId][3*MAX_COL+3]=1;
				else	
					enableEdit[indexId][3*MAX_COL+parseInt(i)-2]=1;
			}				
		}		
	}
}
function setSentTbNam(data)
{
	if (data=="True")
		sentMark[indexId][3*MAX_COL+4]=1;
	else	
		sentMark[indexId][3*MAX_COL+4]=0;
}
function setEditableTbNam(time)
{
	if (time=="None")  return;
	if ((timeNow-time)/60.0 >timeToEdit)
		enableEdit[indexId][3*MAX_COL+4] = 0;	
}
function convertBeforeSend(data,isComment)
{
	if (!isComment) 
		return data
	else
	if (data=="Đ")
		return "D"
	else
	if (data=="CĐ")
		return "CD"
	else	
	if (data=="MG")
		return "duoc mien giam"
	else
		alert("loi");		
}
</script>
